package myuser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import db.*;
public class UserDao {
	private Connection conn;	
	public String sql;
	PreparedStatement ps;
	public UserDao() {
		conn=DBLib.getConn();//连接数据库
	}
	//添加用户
	public void addUser(User user) {
		sql="insert into users (userName,pwd) values (?,?)";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getPwd());
			ps.execute();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//得到用户的信息
	public ArrayList<User> getusers(){
		sql="select * from users";
		ArrayList<User> list=new ArrayList<>();
		try {
			ps=conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			while(rs.next()) {
				User u=new User();
				u.setId(rs.getInt(1));
				u.setUserName(rs.getString(2));
				u.setPwd(rs.getString(3));
				list.add(u);
			}
			conn.close();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;	//所以用户的信息保存在list中
	}
	//判断数据库中是否有该用户
	public boolean checkexists(User user) {
		sql="select * from users where userName=?";
		boolean flag=false;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ResultSet rs=ps.executeQuery();
			if(rs.next()) {
				flag=true;
			}
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	//删除用户
	public void deleteuser(User user) {
		sql="delete from users where id=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, user.getId());
			ps.execute();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//通过id得到该用户的信息
	public User getUserId(int id) {
		sql="select * from users where id=?";
		User user=new User();
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);			
			ResultSet rs=ps.executeQuery();
				while(rs.next()) {
					user.setId(rs.getInt(1));
					user.setUserName(rs.getString(2));
					user.setPwd(rs.getString(3));									
				}
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	//修改用户并保存在数据库
	public void modifyUser(User user) {
		sql="update users set userName=?,pwd=? where id=?" ;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getPwd());
			ps.setInt(3, user.getId());
			ps.execute();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//判断用户是否已经登录
	public boolean checkLogin(User user) {
		sql="select * from users where userName=? and pwd=?";
		boolean flag=false;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getPwd());
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()) {
				flag=true;
			}
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
